package org.supda.system.service.impl;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.supda.system.dao.TMessageDao;
import org.supda.system.entity.TMessage;
import org.supda.system.entity.TMessageSend;
import org.supda.system.entity.TMessageSendId;
import org.supda.system.entity.TRole;
import org.supda.system.entity.TUser;
import org.supda.system.entity.system.DiskEntity;
import org.supda.system.entity.system.SystemPropertySingleton;
import org.supda.system.service.RoleService;
import org.supda.system.service.TMessageSendService;
import org.supda.system.service.TMessageService;
import org.supda.system.util.DataTablePageUtil;

@Service("TMessageService")
@Transactional
public class TMessageServiceImpl implements TMessageService{

	@Autowired
	private TMessageDao messageDao;
	
	private static List<DiskEntity> disks = new ArrayList<DiskEntity>();
	
	private static Sigar sigar = new Sigar();
	
	@Autowired private RoleService roleService;
	
	@Autowired private TMessageSendService messageSendService;
	
	@Override
	public List<DiskEntity> revertAllDisksInfo() {
		
		disks.clear();
		
			FileSystem fslist[];
			try {
				fslist = sigar.getFileSystemList();
			} catch (SigarException e1) {
				return disks;
			}   
	        
	        DecimalFormat df = new DecimalFormat("#0.00");   
	        
	        // String dir = System.getProperty("user.home");// 当前用户文件夹路径  
	        for (int i = 0; i < fslist.length; i++) {   
	            FileSystem fs = fslist[i];   
	          
	            FileSystemUsage usage = null;   
	            try {   
	                usage = sigar.getFileSystemUsage(fs.getDirName());   
	            } catch (SigarException e) {   
	                if (fs.getType() == 2)   
	                continue;   
	            }   
	            switch (fs.getType()) {   
	            case 0: // TYPE_UNKNOWN ：未知  
	                break;   
	            case 1: // TYPE_NONE  
	                break;   
	            case 2: // TYPE_LOCAL_DISK : 本地硬盘  
	            	DiskEntity de = new DiskEntity();
	            	 // 文件系统可用大小
	            	de.setDiskAvail(df.format((float)usage.getAvail()/1024/1024));
	            	// 文件系统剩余大小  
	            	de.setDiskFree(df.format((float)usage.getFree()/1024/1024));
	            	 // 分区的盘符名称  
	            	de.setDiskName(fs.getDevName().replaceAll("\\\\", ""));
	            	de.setDiskReads(df.format((float)usage.getDiskReadBytes()/1024/1024));
	            	// 文件系统总大小  
	            	de.setDiskTotal( df.format((float)usage.getTotal()/1024/1024));
	            	 // 文件系统类型，比如 FAT32、NTFS  
	            	de.setDiskType(fs.getSysTypeName());
	            	de.setDiskWrites(df.format((float)usage.getDiskWriteBytes()/1024/1024));
	            	// 文件系统已经使用量
	            	de.setDiskUsed(df.format((float)usage.getUsed()/1024/1024));
	            	disks.add(de);
	                break;   
	            case 3:// TYPE_NETWORK ：网络  
	                break;   
	            case 4:// TYPE_RAM_DISK ：闪存  
	                break;   
	            case 5:// TYPE_CDROM ：光驱  
	                break;   
	            case 6:// TYPE_SWAP ：页面交换  
	                break;   
	            }   
	        }   
	        return disks;   
	}

	@Override
	public SystemPropertySingleton revertAllSystemProperty() {
		return SystemPropertySingleton.getSystemProperty();
	}

	@Override
	public void save(String content , int retryTimes , Long roleId, String sender, Short messageType) {
		
		TMessage message = 
				findMessageById((Long)messageDao.save(
						new TMessage(
								content, 
								retryTimes, 
								new Timestamp(System.currentTimeMillis()),
								sender,
								messageType)));
		
		if(message != null){
			
			TRole role = roleService.findByRoleId(roleId);
			
			if(role!=null && role.getTUsers()!=null){
				for(TUser user : role.getTUsers()){
					messageSendService.save(new TMessageSend(new TMessageSendId(user.getId(), message.getId()), Short.parseShort("0"), Short.parseShort("0") , 0)) ;
				}
			}
		}
	}

	@Override
	public TMessage findMessageById(Long id) {
		return messageDao.get(TMessage.class, id);
	}

	@Override
	public void listByDataTable(DataTablePageUtil<TMessage> dtp) {
		 messageDao.listByDataTable(dtp, TMessage.class);
	}

}
